from machine import UART
from mpython import *


uart1_SEND_HEAD = b'\xaa\x55'    # head of data
uart1_SEND_FOOT = b'\x55\xaa'    # foot of data
uart1_HEAD_LEN = 2
uart1_FOOT_LEN = 2

TTS_INTEGER_CMD = 0x20          # tts id : signed integer
TTS_DOUBLE_CMD = 0x1f           # tts id : signed double
TTS_CLOCK_CMD = 0x03            # tts id : clock => (hour,minute)
TTS_DATE_CMD = 0x04             # tts id : date => (year,mouth,day)
INTEGER_LEN = 4                 # 4 bytes int
DOUBLE_LEN = 8                  # 8 bytes double
CLOCK_LEN = 2                   # 2 bytes clock(1,1)
DATE_LEN = 6                    # 2 bytes date(4,1,1)


class ASR(object):
    # 语音识别模块初始化rx(蓝) [] tx(绿) []
    def __init__(self,txpin,rxpin):
        self.uart1 = UART(1,115200,tx=txpin,rx=rxpin)
        self.arsCmd = ''

    # 当语音识别有可读数据？(bool)
    def detected(self):
        try:
            if self.uart1.any():
                self.arsCmd = self.uart1.readline()
                self.arsCmd = self.arsCmd.decode()
                if self.uart1.any() > 512: # buffer safe
                    self.uart1.read()
                if not('asr' in self.arsCmd):
                    return 0
                else:
                    return True
        except:
            return 0


    # 语音识别结果 (bool)
    def cmdParse(self):
        try:
            t = self.arsCmd.split('asr')
            return t[1]
        except (UnicodeError, IndexError):
            return -3
        except Exception as err:
            gc.collect()
            return 0


    # 限制数据
    def constrain(self, val, min, max):
        if val < min: return min
        if val > max: return max
        return val


    # 语音播报整数
    def tts_int(self,num):
        buf = bytearray(9)
        num = self.constrain(num, -2**26, 2**26)
        int_to_bytearray = struct.pack('<L', int(num))
        print(int_to_bytearray)
        for i in range(uart1_HEAD_LEN):  
            buf[i+0] = uart1_SEND_HEAD[i]
        buf[2] = TTS_INTEGER_CMD              
        for i in range(INTEGER_LEN):    
            buf[i+3] = int_to_bytearray[i]
        for i in range(uart1_FOOT_LEN):  
            buf[i+7] = uart1_SEND_FOOT[i]
        self.uart1.write(buf) 
        

    # 语音播报双精度小数
    def tts_double(self,f):
        buf = bytearray(13)
        f = self.constrain(f, -2**17, 2**17)
        float_to_bytearray = struct.pack('d', f)
        for i in range(uart1_HEAD_LEN):  
            buf[i+0] = uart1_SEND_HEAD[i]
        buf[2] = TTS_DOUBLE_CMD              
        for i in range(DOUBLE_LEN):    
            buf[i+3] = float_to_bytearray[i]
        for i in range(uart1_FOOT_LEN):  
            buf[i+11] = uart1_SEND_FOOT[i]
        self.uart1.write(buf) 


    # 语音播报日期
    def tts_date(self,y,m,d):
        buf = bytearray(11)
        date_to_bytearray = struct.pack('<iBB',y,m,d)
        print(date_to_bytearray)
        for i in range(uart1_HEAD_LEN):  
            buf[i+0] = uart1_SEND_HEAD[i]
        buf[2] = TTS_DATE_CMD            
        for i in range(DATE_LEN):    
            buf[i+3] = date_to_bytearray[i]
        for i in range(uart1_FOOT_LEN):  
            buf[i+9] = uart1_SEND_FOOT[i]
        self.uart1.write(buf)  
    

    # 语音播报时间
    def tts_clock(self,h,m):
        buf = bytearray(7)
        clock_to_bytearray = struct.pack('BB',h,m)
        for i in range(uart1_HEAD_LEN):  
            buf[i+0] = uart1_SEND_HEAD[i]
        buf[2] = TTS_CLOCK_CMD             
        for i in range(CLOCK_LEN):    
            buf[i+3] = clock_to_bytearray[i]
        for i in range(uart1_FOOT_LEN):  
            buf[i+5] = uart1_SEND_FOOT[i]
        self.uart1.write(buf)  


    # 语音播报语句
    def tts_words(self, id):
        self.uart1.write(bytearray([0xaa, 0x55, id, 0x55, 0xaa]))



        
        
        


  